/*
 * Created by JFormDesigner on Mon May 31 02:57:42 CST 2021
 */

package com.dfrz.bookstore.view;

import com.dfrz.bookstore.entry.User;
import com.dfrz.bookstore.service.impl.UserServiceImpl;
import com.dfrz.bookstore.util.RegularUtiles;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class RetrievePassword extends JFrame {
    UserServiceImpl userService = new UserServiceImpl();
    String varCode;  //存验证码
    User user;
    public RetrievePassword() {
        initComponents();
        setTitle("修改密码");
        sendCode.addActionListener(e -> sendCodeActionPerformed1(e));
        newPassWord.setVisible(false);
        newPassWord2.setVisible(false);
        label5.setVisible(false);
        label6.setVisible(false);
        submitPassword.setVisible(false);
    }

    public void sendCodeActionPerformed1(ActionEvent e ) {
        user = new User();
        user.setLoginName(loginName.getText());
        user.setEmail(emailStr.getText());
        List<User> list = userService.findUserByUser(user);
        if (list.size() <= 0) {
            JOptionPane.showMessageDialog(null,"登录名和邮箱不匹配");
            return;
        }
        /*校验邮箱*/
        if (!RegularUtiles.regEmail(emailStr.getText())){
            return;
        }
        user = list.get(0);
        varCode = userService.sendVarCode(emailStr.getText());
    }


    private void submitCodeActionPerformed(ActionEvent e) {

        if (varCode.equals(inputCode.getText())){
            label4.setVisible(false);
            label1.setVisible(false);
            label3.setVisible(false);
            loginName.setVisible(false);
            emailStr.setVisible(false);
            inputCode.setVisible(false);
            sendCode.setVisible(false);
            submitCode.setVisible(false);

            newPassWord.setVisible(true);
            newPassWord2.setVisible(true);
            label5.setVisible(true);
            label6.setVisible(true);
            submitPassword.setVisible(true);
        }else {
            System.out.println(varCode+inputCode.getText());
            JOptionPane.showMessageDialog(null,"验证码错误");
            varCode = "";//重置验证码
        }
    }

    private void submitPasswordActionPerformed(ActionEvent e) {
        if (newPassWord.getText().equals(newPassWord2.getText())){
            user.setPassWord(newPassWord.getText());
//            System.out.println(user);
            userService.updateUser(user);
            dispose();
        }else {
            JOptionPane.showMessageDialog(null,"两个密码不一样");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        emailStr = new JTextField();
        inputCode = new JTextField();
        sendCode = new JButton();
        label4 = new JLabel();
        loginName = new JTextField();
        submitPassword = new JButton();
        newPassWord = new JTextField();
        newPassWord2 = new JTextField();
        label5 = new JLabel();
        label6 = new JLabel();
        submitCode = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
            panel1.setLayout(null);

            //---- label1 ----
            label1.setText("\u90ae\u7bb1:");
            label1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
            panel1.add(label1);
            label1.setBounds(55, 155, 75, label1.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u627e\u56de\u5bc6\u7801");
            label2.setHorizontalAlignment(SwingConstants.CENTER);
            label2.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 18));
            panel1.add(label2);
            label2.setBounds(160, 30, 165, label2.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u9a8c\u8bc1\u7801:");
            label3.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
            panel1.add(label3);
            label3.setBounds(50, 215, 70, label3.getPreferredSize().height);
            panel1.add(emailStr);
            emailStr.setBounds(140, 155, 210, emailStr.getPreferredSize().height);
            panel1.add(inputCode);
            inputCode.setBounds(140, 210, 210, 24);

            //---- sendCode ----
            sendCode.setText("\u53d1\u9001\u9a8c\u8bc1\u7801");
            panel1.add(sendCode);
            sendCode.setBounds(new Rectangle(new Point(375, 210), sendCode.getPreferredSize()));

            //---- label4 ----
            label4.setText("\u767b\u5f55\u540d:");
            label4.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
            panel1.add(label4);
            label4.setBounds(new Rectangle(new Point(52, 90), label4.getPreferredSize()));
            panel1.add(loginName);
            loginName.setBounds(142, 89, 205, loginName.getPreferredSize().height);

            //---- submitPassword ----
            submitPassword.setText("\u4fee\u6539\u5bc6\u7801");
            submitPassword.addActionListener(e -> submitPasswordActionPerformed(e));
            panel1.add(submitPassword);
            submitPassword.setBounds(175, 285, 135, submitPassword.getPreferredSize().height);
            panel1.add(newPassWord);
            newPassWord.setBounds(155, 125, 185, newPassWord.getPreferredSize().height);
            panel1.add(newPassWord2);
            newPassWord2.setBounds(155, 180, 185, 24);

            //---- label5 ----
            label5.setText("\u65b0\u5bc6\u7801:");
            label5.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
            panel1.add(label5);
            label5.setBounds(new Rectangle(new Point(95, 125), label5.getPreferredSize()));

            //---- label6 ----
            label6.setText("\u786e\u5b9e\u5bc6\u7801:");
            label6.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
            panel1.add(label6);
            label6.setBounds(75, 185, 59, 16);

            //---- submitCode ----
            submitCode.setText("\u63d0\u4ea4");
            submitCode.addActionListener(e -> submitCodeActionPerformed(e));
            panel1.add(submitCode);
            submitCode.setBounds(175, 250, 135, submitCode.getPreferredSize().height);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(5, 5, 505, 345);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField emailStr;
    private JTextField inputCode;
    private JButton sendCode;
    private JLabel label4;
    private JTextField loginName;
    private JButton submitPassword;
    private JTextField newPassWord;
    private JTextField newPassWord2;
    private JLabel label5;
    private JLabel label6;
    private JButton submitCode;
    // JFormDesigner - End of variables declaration  //GEN-END:variables



}
